package com.changgou.comment.service.impl;

import com.changgou.comment.service.CommentService;
import com.changgou.pojo.SkuComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Project: com.changgou.comment.service.impl
 * @Author: ShaoDi Wang
 * @Date: Created in 2019-08-11 21:08
 * @Description:
 * @Version: 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 新增
     * @param skuComment
     */
    @Override
    public void add(SkuComment skuComment) {
        mongoTemplate.save(skuComment);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<SkuComment> findAll() {
        return mongoTemplate.findAll(SkuComment.class);
    }

    /**
     * 根据orderId查询
     * @param id
     * @return
     */
    @Override
    public SkuComment findById(String id) {
        return mongoTemplate.findById(id,SkuComment.class);
    }

    /**
     * 更新
     * @param skuComment
     */
    @Override
    public void update(SkuComment skuComment) {
        Query query =
                Query.query(Criteria.where("orderId").is(skuComment.getOrderId()));
        Update update = Update.update("imgUrl", "http://www.itheima.com").set(
                "anonymous", true);
        mongoTemplate.updateFirst(query, update, SkuComment.class);
    }

    /**
     * 根据orderId查询
     * @param id
     */
    @Override
    public void delete(String id) {
        Query query = Query.query(Criteria.where("orderId").is(id));
        mongoTemplate.remove(query, SkuComment.class);
    }

    @Override
    public List<SkuComment> findList(Map searchMap) {
        return null;
    }

    @Override
    public Page<SkuComment> findPage(Map searchMap, int page, int size) {
        return null;
    }
}
